import Vue from "vue"
import App from "./App.vue"
import router from "router/index.js"
import store from "store/index.js"
import "default-passive-events"

import "styles/normalize.css"
import Mixins from "config/mixins.js"
import XRadio from "@/components/Radio/index.vue"
import XDialog from "@/components/Dialog/index.vue"
import XLoading from "@/components/Loading/index.vue"
import XSvgIcon from "@/components/SvgIcon/index.vue"
import XBreadcrumb from "@/components/Breadcrumb/index.vue"
import VueBus from "vue-bus"
import Xwangeditor from "@/components/wangeditor/editor.vue"
import XcropperFixed from "@/components/cropper/bannerImg.vue"
import SearchCondition from "@/components/searchCondition/index.vue"
import DeletetkPage from "@/components/Deletebtn/Deletetk.vue"
import AgreeDialog from "@/components/agreeDialog2/index.vue"
import SigninDialog from "@/components/signinDialog/index.vue"
// Element-ui
import ElementUI from "element-ui"
import "styles/element-ui.scss"
import "styles/common.scss"
import { imgUrl, upUrl } from "../config/env.js"
//vue-quill-editor修改工具
import "quill/dist/quill.core.css" // import styles
import "quill/dist/quill.snow.css" // for snow theme
import "quill/dist/quill.bubble.css" // for bubble theme
import moment from "moment"
import "moment/locale/zh-cn"
moment.locale("zh-cn")
import { errimgurl } from "./utils/commonFunc.js"
import isFirstInPage from "./utils/FirstInPage.js"

//console.log(errimgurl)
Vue.prototype.$errImg = errimgurl
import VueCropper from "vue-cropper"
import printJS from "print-js"
Vue.prototype.$isFirstInPage = isFirstInPage

Vue.use(VueCropper)
Vue.use(ElementUI, { size: "small", zIndex: 3000 })
Vue.use(elFunc)
Vue.use(VueBus)
Vue.prototype.moment = moment
Vue.prototype.$img = function (img) {
  if (img?.includes?.(":/")) {
    return img
  } else {
    return imgUrl + img
  }
}
Vue.prototype.$upUrl = upUrl
Vue.prototype.$print = printJS

Vue.prototype.$ispacket = localStorage.getItem("ispacket") //是否显示红包或是精美礼品 1 是不显示红包 2 是不显示礼品  其他的都是全部显示 // 有1就有实物礼品，如果有2就有红包礼品
Vue.prototype.$ispacket2 = localStorage.getItem("ispacket2")

Vue.prototype.$isborder = true //书否显示talble 线
// 80  160  240
Vue.prototype.$widtha = 80 //表格宽度 类型1
Vue.prototype.$widths = 120 //表格宽度 类型1
Vue.prototype.$widthb = 160 //表格宽度 类型2
Vue.prototype.$widthc = 240 //表格宽度 类型3   //操作栏比较特殊，根据情况调整
Vue.prototype.$headerCellStyle = {
  background: "#d8d8d8",
  color: "#000000",
  textAlign: "left",
} //表格表头样式

// :header-align="$heaerOpt"
Vue.prototype.$heaerOpt = "center" //   left/center/right
// ECharts
import * as echarts from "echarts/core"
import { TooltipComponent, LegendComponent, TitleComponent, ToolboxComponent, GridComponent } from "echarts/components"
import { PieChart, LineChart, BarChart } from "echarts/charts"
import { SVGRenderer } from "echarts/renderers"
// import { from } from "core-js/core/array";
echarts.use([
  PieChart,
  LineChart,
  TooltipComponent,
  LegendComponent,
  TitleComponent,
  ToolboxComponent,
  GridComponent,
  SVGRenderer,
  BarChart,
])
Vue.prototype.$echarts = echarts

// 全局组件
Vue.component("x-radio", XRadio)
Vue.component("x-dialog", XDialog)
Vue.component("x-loading", XLoading)
Vue.component("x-svg-icon", XSvgIcon)
Vue.component("x-breadcrumb", XBreadcrumb)
Vue.component("x-wangeditor", Xwangeditor)
Vue.component("x-cropperFixed", XcropperFixed)
Vue.component("SearchCondition", SearchCondition)
Vue.component("DeletetkPage", DeletetkPage)
Vue.component("x-AgreeDialog", AgreeDialog)
Vue.component("x-SigninDialog", SigninDialog)

// 自定义组件
Vue.directive("alive", {
  inserted(el, binding) {
    let tagText = el.innerText.trim()
    let routeMeta = router.history.current?.meta
    const btnRule = routeMeta.btnRule?.join(",")?.split(",")
    if (binding.value) {
      console.log(binding.value)
    } else if (!btnRule?.includes(tagText)) {
      el?.parentNode?.removeChild(el)
    }
  },
})
Vue.mixin(Mixins)
const requireAll = (reqContext) => reqContext.keys().map(reqContext)
const reqContext = require.context("./assets/icon", false, /\.svg$/)
requireAll(reqContext)
let tokenx = sessionStorage.getItem("token")
if (tokenx) {
  // let a = JSON.parse(tokenx)
  // a.expire_time
  // let tokenTime = Date.parse(a.expire_time)
  // let endTime = Date.parse(new Date())
  // console.log(new Date())
  // console.log(tokenTime < endTime)
  // if (tokenTime < endTime) {
  //   localStorage.removeItem('token')
  //   localStorage.removeItem('authorityTree')
  // }
} else {
  localStorage.removeItem("token")
  localStorage.removeItem("authorityTree")
}

router.beforeEach((to, from, next) => {
  // 登录验证
  if (to.path === "/login") {
    next()
  } else if (localStorage.getItem("token")) {
    next()
  } else if (to.matched.some((record) => record.meta.notRequiresAuth)) {
    next()
  } else {
    next({ name: "Login", params: { action: "refresh" } })
  }
  // 首页判定
  if (to.path === "/") {
    let authorityTree = localStorage.getItem("authorityTree")
    if (authorityTree.includes("/index")) {
      next("/index")
    } else {
      next("/welcome")
    }
  }
})

Vue.config.productionTip = false
new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app")
